package com.example.mywike.user.service;

import com.example.mywike.config.shiroConfig.ShiroConfigStingInteger;
import com.example.mywike.user.entity.inputEntity.UserLoginInput;
import com.example.mywike.utils.returnUtils.ReturnData;
import com.example.mywike.utils.returnUtils.ReturnUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

/**
 * @Author xiyanqiulvgui
 * @Date 2020/9/12 22:25
 * @Description 用户登录业务
 * @Version 1.0.0
 */
@Slf4j
@Service
public class UserLoginServiceImpl implements UserLoginService {

    /**
     * @param userLoginInput 用户输入信息
     * @author xiyangqilvgui
     * @decription
     * @date 2020/9/12 22:29
     * @version 1.0.0
     * @return  返回通用对象
     * */
    @Override
    public ReturnData login(UserLoginInput userLoginInput) {
        log.info("userLoginInput = {}" , userLoginInput);

        // 创建shiro主体
        Subject subject = SecurityUtils.getSubject();
        // 封装user信息
        String userName = userLoginInput.getIdentifier()+ShiroConfigStingInteger.DELIMITER+userLoginInput.getIdentityType();
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName,userLoginInput.getCredential());

        // 是否记住我
        userToken.setRememberMe(userLoginInput.isRememberMe());

        // 将token传给主体
        subject.login(userToken);

        userToken.setUsername(userLoginInput.getIdentifier());
        return ReturnUtils.success(userToken);
    }
}
